package com.sdp.wx.flow;

import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.wx.db.wx_assets_info.entity.WxAssetsInfo;
import com.sdp.wx.db.wx_assets_info.service.IWxAssetsInfoService;
import com.sdp.wx.db.wx_assets_scrap_apply.entity.WxAssetsScrapApply;
import com.sdp.wx.db.wx_assets_scrap_apply.service.IWxAssetsScrapApplyService;
import com.sdp.wx.db.wx_assets_scrap_report_detail.entity.WxAssetsScrapReportDetail;
import com.sdp.wx.db.wx_assets_scrap_report_detail.service.IWxAssetsScrapReportDetailService;
import com.sdp.wx.utils.WxConstants;

public class AssetsScrapApplyEndNodeTaskListener implements TaskListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateTask delegateTask) {
		String isPass = (String) delegateTask.getVariable("isPass");
		WxAssetsScrapApply report = (WxAssetsScrapApply) delegateTask.getVariable("flowData");
		if (!StringUtils.isEmpty(isPass) && WxConstants.是.getCode().equals(isPass)
				&& report != null) {
			
			IWxAssetsScrapApplyService assetsScrapApplyService = ApplicationContextUtils.getBean(IWxAssetsScrapApplyService.class);
			UpdateWrapper<WxAssetsScrapApply> updateWrapper = new UpdateWrapper<>();
			updateWrapper.lambda().eq(WxAssetsScrapApply::getKeyId, report.getKeyId())
				.set(WxAssetsScrapApply::getApplyStatus, WxConstants.流程审批状态_审批通过.getCode());
			assetsScrapApplyService.update(updateWrapper);
			
			//更新固定资产报告从表
			IWxAssetsScrapReportDetailService assetsScrapReportDetailService = ApplicationContextUtils.getBean(IWxAssetsScrapReportDetailService.class);
			UpdateWrapper<WxAssetsScrapReportDetail> updateWrapperDetail = new UpdateWrapper<>();
			updateWrapperDetail.lambda().eq(WxAssetsScrapReportDetail::getKeyId, report.getReportDetailId())
				.set(WxAssetsScrapReportDetail::getScrapStatus, WxConstants.是.getCode());
			assetsScrapReportDetailService.update(updateWrapperDetail);
			
			//更新固定资产表报废状态
			IWxAssetsInfoService assetsInfoService = ApplicationContextUtils.getBean(IWxAssetsInfoService.class);
			UpdateWrapper<WxAssetsInfo> updateWrapperAssets = new UpdateWrapper<>();
			updateWrapperAssets.lambda().eq(WxAssetsInfo::getAssetNo, report.getAssetNo())
				.set(WxAssetsInfo::getScrapStatus, WxConstants.是.getCode());
			assetsInfoService.update(updateWrapperAssets);
		}

	}

}
